package cn.mf.hrm.mq.consumer;

import cn.mf.hrm.dto.EmailSendDto;
import cn.mf.hrm.dto.MsgSendDto;
import cn.mf.hrm.dto.SmsgSendDto;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import static cn.mf.hrm.constant.MsgQueueConstant.*;
//消息消费
@Component
@Slf4j
public class MessageConsumer {

    //1:短信消息处理
    @RabbitListener(queues = QUEUE_MSG , containerFactory = "rabbitListenerContainerFactory")
    public void handleSmgMesssage(@Payload MsgSendDto dto , Message message , Channel channel) throws IOException {
        log.info("消费者收到消息 - {} " , dto);
        //2:发送短信
        dto.getPhones().forEach(phone->{
            log.info("调用短信网关，执行短信发送 ：phone = {} , content= {} ", phone , dto.getContent());
        });
        //3:签收消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //1:邮件消息处理
    @RabbitListener(queues =QUEUE_EMAIL , containerFactory = "rabbitListenerContainerFactory")
    public void handleEmailMesssage(@Payload EmailSendDto dto , Message message , Channel channel) throws IOException {
        log.info("消费者收到消息 - {} " , dto);
        //2:发送短信
        dto.getEmails().forEach(email->{
            log.info("调用邮件API，执行邮件发送 ：email = {} , content= {} ", email , dto.getContent());
        });
        //3:签收消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //1:站内信消息处理
    @RabbitListener(queues = QUEUE_SMSG , containerFactory = "rabbitListenerContainerFactory")
    public void handleSmsgMesssage(@Payload SmsgSendDto dto , Message message , Channel channel) throws IOException {
        log.info("消费者收到消息 - {} " , dto);
        dto.getUserIds().forEach(userId->{
            log.info("保存站内信到数据库 userId= {} ,  content= {} , title ={} ,type={} ",
                    userId,dto.getContent() , dto.getTitle() , dto.getType());
        });
        //2:签收消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
